package com.eleme.service;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.eleme.dbutil.Dbutil;
import com.eleme.entity.Bussiness;


public class NormalService {
	//
	private List<Bussiness> change(String sql,Object[]params){
		ResultSet rs=Dbutil.query(sql, params);
		List<Bussiness> buss=new ArrayList<Bussiness>();
		try {
			//通过next来索引：判断是否有下一个记录
			while (rs.next()) {
				Bussiness bs = new Bussiness();
				bs.setId(rs.getInt("id"));
				bs.setName(rs.getString("name"));
				bs.setLevel(rs.getInt("level"));
				bs.setParentid(rs.getInt("parentid"));
			buss.add(bs);
			}
			System.out.println("查询完毕，释放掉占用的资源。");
			rs.close();//关闭ResultSet对象
		} catch (Exception e) {
			// TODO: handle exception
		}
		return buss;
	}

//执行SQL语句
	public List<Bussiness> findparentname(){
		String sql="select * from bussiness where level=0 order by id";
		String sql1="select * from bussiness where level != 0 order by parentid";
		List<Bussiness> fat = change(sql, null);
		List<Bussiness> son = change(sql1, null);
		for(Bussiness fatBus:fat) {
			for(Bussiness sonBus:son) {
				if(fatBus.getId() == sonBus.getParentid()) {
					fatBus.getLstChild().add(sonBus);
					
				}
			}
		}

		return fat;
	}	


	//模糊搜索
	private String cond="";
	public String getCond() {
		return cond;
	}
	public void setCond(String cond) {
		this.cond = cond;
	}
	public String getConditionSQL(){
		String sql="";
		if (cond!=null) {
			//分割语句
			String [] conds=cond.split(",");
			for(String cond:conds){
				if (cond.indexOf("#=")!=-1) {
					String[]kv=cond.split("#=");
					sql+=""+kv[0]+" like '%"+kv[1]+"%' ";
				}else if(cond.indexOf("-")!=1){
					sql+=""+cond.replace("=", " between ").replace("-", " add ");
				}else{
					sql+=" "+cond;
				}
				sql+=" and ";
			}
		}
		sql=sql.replaceFirst("( and )$", "");
		return sql;
	}
	
}











	












